/**
 * Classe responsavel pela gerencia do mundo
 */
import corejuliano.designpattern.mvc.*; 
import util.xpath.*;
import util.file.*;
import util.*;
import corejuliano.gameelement.*;
import flash.geom.*;
import corejuliano.gameelement.agent.*;
import corejuliano.gameelement.perspective.*;
import corejuliano.*;
import corejuliano.gameelement.world.*;
 
class corejuliano.gameelement.world.World extends Controller implements  XMLParserObserver
{
	private var mapTiles:Array;
	private var spacing:Number = 0;
	private var map:Array;
	private var width:Number;
	private var height:Number;
	private var depthTile:Number = 0;
	private var depthInterval:Number = 5;
	
	public function World(id:String, xml:String, way:String, spacing:Number)
	{
		super(id);
		mapTiles = new Array();
		map = new Array();
		var xmlParse:XMLParser = new XMLParser(xml,way);
		xmlParse.addObserver(this);
		setSpacing(spacing);		
	}
	
	public function onLoadDataXMLParser(xmlParser:XMLParser):Void
	{
		var xml:XML = xmlParser.getXML();
		var mapstr:String = XPathAPI.selectSingleNode(xml.firstChild, "phase/map").firstChild.nodeValue;
		map = mapstr.split(",");
		width = XPathAPI.selectSingleNode(xml.firstChild, "phase/map").attributes.width;
		height = XPathAPI.selectSingleNode(xml.firstChild, "phase/map").attributes.height;
		mountMapTiles();
		GameManager.getInstance().getActivePhase().startEspecifyPhase();
	}
	
	public function mountMapTiles():Void
	{
		for (var i:Number = 0; i < height; i++)
		{
			for(var j:Number = width -1; j >= 0; j--)
			{
				var tile:Tile = new Tile();
				tile.setPosition(new Point(j, i));
				depthTile += depthInterval;
				tile.setDepth(depthTile);
				var id:String = map[(i * width) + j];
				createElement(id.split("")[0],tile,j,i);
				mapTiles[(i * width) + j] = tile;
			}
		}
	}
		
	
	public function createElement(id:String, tile:Tile, j:Number, i:Number):Void
	{
		if (id == "w")
		{
			//var phase:Phase = GameManager.getInstance().getActivePhase();
			//phase.createWall (id, tile, getWorldTopDownCanvas());
		}
	}
	
	public function getWorldTopDownCanvas():MovieClip
	{
		//pega as view do controller, ja queo troco e um controller :D
		for (var i:Number = 0; i< getViews().length; i++)
			if (getViews()[i] instanceof WorldTopDownView)
				return WorldTopDownView(getViews()[i]).getWorldTopDownCanvas();
	}
	
	public function getIsometricWorldCanvas():MovieClip
	{
		for (var i:Number = 0; i< getViews().length; i++)
			if (getViews()[i] instanceof WorldIsometricView)
				return WorldIsometricView(getViews()[i]).getWorldIsometricCanvas();
	}
	
	public function moveTo (targetPosition:Point, agent:Agent):Boolean
	{
		var targetTile:Tile = getTileByPosition(targetPosition);
		if (targetTile.isSolid())
			return false;
			
		agent.setTile(targetTile);
		return true;	
	}
	
	public function destroyMapTiles():Void
	{
		for (var i:Number = 0; i < height; i++)
		{
			for (var j:Number = width -1; j > 0 ; j--)
			{
				var tileables:Array = mapTiles[(i * width) +j].getTileables();
				for (var x:Number = 0; x < tileables.length; x++)
					tileables[x].destroy();
			}
		}
	}
	
	public function getTileByPosition(position:Point):Tile
	{
		return mapTiles[(position.y * width) + position.x];		
	}

	public function getSpacing():Number
	{
		return spacing;
	}

	public function setSpacing(spacing:Number):Void
	{
		this.spacing = spacing;
	}
	
	public function getMapTiles():Array
	{		
		return  mapTiles;
	}
	
	public function toString():String
	{
		return "corejuliano.gameelement.world.World";
	}

}